<?php
namespace Home\Controller;
use Think\Controller;

class VzLoanProtocolController extends CommonController {
    /*
     * 借款协议
     */
    public function VzLoanProtocol(){
        $get = I('data');
        $get = $this->jiemi($get);
//        $get['user_id']=21957;
//        $get['shop_identifier']='0100010030';
        //$vz_loan=M('vz_loan','','DB2')->where(['order_number'=>$get['order_number']])->field('order_number,loan_amount,loan_type,fee,overdue_money,atlast_time,ctime,lender_id,param_id')->find();
        $user=M('user','','DB2')->where(['id'=>$get['user_id']])->field('username,mobile,identity_num,address,channel')->find();
       /*
        * 按顺序分配出借人
        */
        $channel=M('channel','','DB2')->where(['chan_id'=>$user['channel']])->field('lender_id')->find();
        $lender=M('lender','','DB2')->where(['id'=>$channel['lender_id'],'is_delete'=>1])->field('name,mobile,idcard')->find();
        if($channel&&$lender){

        }else{
            $lender_id=M('lender','','DB2')->where(['shop_idenfier'=>$get['shop_identifier'],'is_delete'=>1])->getField('id');
            $lender_id_desc=M('lender','','DB2')->where(['shop_idenfier'=>$get['shop_identifier'],'is_delete'=>1])->order('id desc')->getField('id');
            $lender_distribution=M('lender_distribution','','DB2')->where(['shop_identifier'=>$get['shop_identifier']])->order('id desc')->getField('lender_id');
            if(empty($lender_distribution)){
                $lender=M('lender','','DB2')->where(['id'=>$lender_id])->field('name,mobile,idcard')->find();
            }else{
                if($lender_distribution<$lender_id_desc){
                    $lender=M('lender','','DB2')->where(['id'=>['gt',$lender_id_desc],'shop_idenfier'=>$get['shop_identifier']])->field('name,mobile,idcard')->find();
                }elseif($lender_distribution==$lender_id_desc){
                    $lender=M('lender','','DB2')->where(['id'=>['eq',$lender_id_desc]])->field('name,mobile,idcard')->find();
                }else{
                    $lender=M('lender','','DB2')->where(['id'=>$lender_id])->field('name,mobile,idcard')->find();
                }
            }
        }
        /*
        * 按顺序分配出借人
        */
        $param_id=M('merchant_param_record','','DB2')->where(['shop_identifier'=>$get['shop_identifier']])->order('id desc')->field('day_interest')->find();

        $vz_loan['borrower']=$user;//借款人信息
        $vz_loan['lender']=$lender;//出借人信息//$param_id['day_interest']*365*100;
        $vz_loan['interest_rate']=empty($param_id['day_interest'])?0:round(($param_id['day_interest']/10000)*365,2);//年利率
        switch ($vz_loan['loan_type']){
            case 1:$vz_loan['loan_type']='日常消费';
                break;
            case 2:$vz_loan['loan_type']='医疗';
                break;
            case 3:$vz_loan['loan_type']='旅游';
                break;
            case 4:$vz_loan['loan_type']='装修';
                break;
            case 5:$vz_loan['loan_type']='教育';
                break;

        }

        $data['ret'] = '1001';
        $data['code'] = 'success';
        $data['data']=$vz_loan;
        $data = $this->jiami($data);
        $this->return = ['data' => $data];
        return false;
    }
    
}